%%% ------------------------------------------------------------------
%%% Licensed under the Apache License, Version 2.0 (the 'License');
%%%  you may not use this file except in compliance with the License.
%%%  You may obtain a copy of the License at
%%%
%%%      http://www.apache.org/licenses/LICENSE-2.0
%%%
%%% Copyright (c) 2018 dwg <bypf2009@vip.qq.com>
%%%
%%%  Unless required by applicable law or agreed to in writing, software
%%%  distributed under the License is distributed on an 'AS IS' BASIS,
%%%  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%%%  See the License for the specific language governing permissions and
%%%  limitations under the License.
%%%
%%% @doc
%%% @author  dwg <'bypf2009@vip.qq.com'>
%%% @copyright 2018 dwg <bypf2009@vip.qq.com>
%%% @end
%%% created|changed : 2018-11-27 19:28
%%% coding : utf-8
%%% ------------------------------------------------------------------
-module(describe_domains_test).
-author("dwg").
-export([mtest_1/0, mtest_2/0, mtest_3/0]).
-export([mtest_4/0, mtest_5/0, mtest_6/0]).

-define(AccessKeyId, <<"LTAILO9m0q3aow4R">>).
-define(AccessKeySecret, <<"TS1DL0Am8AOq8LS1jm7abqAcVDNLNk">>).
-define(DOMAIN, <<"zyxjob.cn">>).
-define(DOMAIN_RR, <<"alitest">>).

mtest_1() ->
    ParamsExtra = #{
        <<"Action">> => <<"DescribeDomainRecords">>,
        <<"DomainName">> => ?DOMAIN,
        <<"PageNumber">> => <<"1">>,
        <<"PageSize">> => <<"20">>
        % "RRKeyWord" => www,
        % <<"TypeKeyWord">> => <<"MX">>,
        % <<"ValueKeyWord">> => <<"com">>
    },
    domain_api:describe_domains_records(?AccessKeyId, ?AccessKeySecret, ParamsExtra).


mtest_2() ->
    #{<<"DomainRecords">> := #{<<"Record">> := Domains}} = mtest_1(),
    [#{<<"RecordId">> := RecordId}|_] = Domains,
    ParamsExtra = #{
        <<"Action">> => <<"DescribeDomainRecordInfo">>,
        <<"RecordId">> => RecordId
    },
    domain_api:describe_domains_record_info(?AccessKeyId, ?AccessKeySecret, ParamsExtra).

mtest_3() ->
    ParamsExtra = #{
        <<"Action">>     => <<"DescribeSubDomainRecords">>,
        <<"SubDomain">>  => <<"api.zyxjob.cn">>,
        <<"PageNumber">> => <<"1">>,
        <<"PageSize">>   => <<"2">>,
        <<"Type">>       => <<"AAAA">>
    },
    domain_api:describe_sub_domains_records(?AccessKeyId, ?AccessKeySecret, ParamsExtra).


mtest_4() ->
    ParamsExtra = #{
        <<"Action">> => <<"AddDomainRecord">>,
        <<"DomainName">> => ?DOMAIN,
        <<"RR">>     => ?DOMAIN_RR,
        <<"Type">>   => <<"A">>,
        <<"Value">>  => <<"1.119.151.34">>
        % <<"TTL">>    => <<"600">>
        % <<"Priority">>   => <<"10">>
    },
    domain_api:add_domain_record(?AccessKeyId, ?AccessKeySecret, ParamsExtra).

mtest_5() ->
    #{<<"DomainRecords">> := #{<<"Record">> := Domains}} = mtest_1(),
    RecordId1 = lists:foldl(
        fun
            (#{<<"DomainName">> := ?DOMAIN,
               <<"RR">>         := ?DOMAIN_RR,
               <<"RecordId">>   := RecordId}, _) -> RecordId;
            (_, RecordId)                        -> RecordId
        end, <<>>, Domains),
    ParamsExtra = #{
        <<"Action">> => <<"DeleteDomainRecord">>,
        <<"RecordId">> => RecordId1
    },
    domain_api:delete_domain_record(?AccessKeyId, ?AccessKeySecret, ParamsExtra).

mtest_6() ->
    mtest_4(),
    #{<<"DomainRecords">> := #{<<"Record">> := Domains}} = mtest_1(),
    RecordId1 = lists:foldl(
        fun
            (#{<<"DomainName">> := ?DOMAIN,
               <<"RR">>         := ?DOMAIN_RR,
               <<"RecordId">>   := RecordId}, _) -> RecordId;
            (_, RecordId)                        -> RecordId
        end, <<>>, Domains),
    ParamsExtra = #{
        <<"Action">> => <<"UpdateDomainRecord">>,
        <<"RecordId">> => RecordId1,
        <<"RR">>     => ?DOMAIN_RR,
        <<"Type">>   => <<"A">>,
        <<"Value">>  => <<"192.168.199.102">>
        % <<"TTL">>,
        % <<"Priority">>,
        % <<"Line">>
    },
    domain_api:update_domain_record(?AccessKeyId, ?AccessKeySecret, ParamsExtra).
